package cc.aiee.demo.springboot.core;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.beanstream.Gateway;
import com.braintreegateway.BraintreeGateway;

import cc.aiee.bambora.northamerica.config.BamboraPayConfig;
import cc.aiee.bambora.northamerica.config.BamboraPayConfigKit;
import cc.aiee.chinapay.config.ChinaPayConfig;
import cc.aiee.chinapay.config.ChinaPayConfigKit;
import cc.aiee.demo.springboot.basic.properties.BamboraPayProperties;
import cc.aiee.demo.springboot.basic.properties.ChinaPayProperties;
import cc.aiee.demo.springboot.basic.properties.MonerisProperties;
import cc.aiee.demo.springboot.basic.properties.PayPalProperties;
import cc.aiee.demo.springboot.basic.properties.WxPayProperties;
import cc.aiee.moneris.config.MonerisPayConfig;
import cc.aiee.moneris.config.MonerisPayConfigKit;
import cc.aiee.paypal.config.PayPalConfig;
import cc.aiee.paypal.config.PayPalConfigKit;
import cc.aiee.wxpay.config.WxPayConfig;
import cc.aiee.wxpay.config.WxPayConfigKit;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MyApplicationRunner implements ApplicationRunner {

	@Autowired
	private PayPalProperties payPalProperties;

	@Autowired
	private ChinaPayProperties chinaPayProperties;

	@Autowired
	private BamboraPayProperties bamboraPayProperties;
	@Autowired
	private MonerisProperties monerisProperties;
	
	@Autowired
	private WxPayProperties wxPayProperties;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		log.info("SpringBoot启动成功执行初始化");

		BraintreeGateway defaultBraintreeGateway = PayPalConfigKit.defaultBraintreeGateway;
		if (defaultBraintreeGateway == null) {
			PayPalConfig payPalConfig = new PayPalConfig();
			BeanUtils.copyProperties(payPalProperties, payPalConfig);

			log.info("实例化PayPal参数信息:\n{}", payPalConfig.toString());

			PayPalConfigKit.setDefault(payPalConfig, payPalConfig.isSandbox());

		}
		ChinaPayConfig config = ChinaPayConfigKit.config;
		if (config == null) {
			config = new ChinaPayConfig();
			config.setVerifyFile(chinaPayProperties.getVerifyFile());
			config.setSignFilePassword(chinaPayProperties.getPassword());
			config.setSignFile(chinaPayProperties.getSignFile());
			config.setSandbox(chinaPayProperties.isOpenSandbox());
			config.setMerchantId(chinaPayProperties.getMerchantId());
			config.setMerBgUrl(chinaPayProperties.getMerBgUrl());
			config.setMerBgRefundUrl(chinaPayProperties.getMerBgRefundUrl());
			ChinaPayConfigKit.config = config;
			log.info("实例化ChinaPay配置:\n{}", config.toString());
		}

		Gateway defaultGateway = BamboraPayConfigKit.defaultGateway;
		if (defaultGateway == null) {
			BamboraPayConfig bamboraConfig = new BamboraPayConfig();
			bamboraConfig.setApiPasscodes(bamboraPayProperties.getApiPwd());
			bamboraConfig.setMerchantId(bamboraPayProperties.getMerchantId());
			bamboraConfig.setVersion(bamboraConfig.getVersion());
			BamboraPayConfigKit.setDefault(bamboraConfig, bamboraPayProperties.getOpenSandbox());

			log.info("Bambora Pay 初始化配置:\n{}", bamboraConfig.toJson());
		}
		MonerisPayConfig defaultMoneris = MonerisPayConfigKit.getDefaultConfig();
		if (defaultMoneris == null) {
			MonerisPayConfig conf = new MonerisPayConfig();
			conf.setApiToken(monerisProperties.getApiToken());
			conf.setSandbox(monerisProperties.getOpenSandbox());
			conf.setProcCountryCode(monerisProperties.getProcCountryCode());
			conf.setStoreId(monerisProperties.getStoreId());
			MonerisPayConfigKit.setDefault(conf, conf.isSandbox());
		}

		if(wxPayProperties != null) {
			WxPayConfig conf = new WxPayConfig();
			conf.setApiSecret(wxPayProperties.getApiSecret());
			conf.setAppId(wxPayProperties.getAppId());
			conf.setCertPath(wxPayProperties.getCertPath());
			conf.setCertPwd(wxPayProperties.getCertPwd());
			conf.setMchId(wxPayProperties.getMchId());
			conf.setNotifyUrl(wxPayProperties.getNotifyUrl());
			conf.setNotifyUrlRefund(wxPayProperties.getNotifyUrlRefund());
			WxPayConfigKit.setDefaultConfig(conf);
			log.info("微信支付配置:{}",conf.toJson(true, false));
		}
		
	}

}
